var SpriteManager = require("SpriteManager");

cc.Class({
    extends: cc.Component,

    properties: {
        scoreDisplay: {
            default: null,
            type: cc.Label
        },
		messageDisplay: {
            default: null,
            type: cc.Label
        },
		nextDisplay: {
            default: null,
            type: cc.Label
        },
		backDisplay: {
            default: null,
            type: cc.Label
        }
    },

     onLoad () {
		 
		   this.spriteManager = new SpriteManager();
		 
		   this.scoreAddCallback = cc.callFunc(function () {
				this.addStart();
		   }, this); 
		   
           		   
		   this.score = window.score;
		   this.levelTarget = window.target;
		   this.level = window.level ;
		   this.lastScore = 0;
		   this.passLevel = window.passLevel;
		   
		   this.scoreAdd = Math.floor(this.score/120)+1;
		 
		   this.scoreDisplay.string = '本局得分 : 0';
		   
		   this.scoreDisplay.node.setPosition(300,50);
		   
		   this.scoreDisplay.node.runAction(cc.sequence(cc.moveBy(0.2,-300,0),cc.delayTime(0.1),this.scoreAddCallback ));
		   
		   this.flag = true;
		   
		   this.messageDisplay.node.active = false;
		   this.nextDisplay.node.active = false;
		   this.backDisplay.node.active = false;
		   
		   
	 },
	 
	 update(dt){
		 if(!this.flag){
			 
			// console.log(this.flag);
			 
			 if(this.lastScore<this.score){
				 if(this.score-this.lastScore>this.scoreAdd)
					this.lastScore+=this.scoreAdd;
				 else
					 this.lastScore=this.score;
				 this.scoreDisplay.string = '本局得分 : '+this.lastScore;
			 }else{
				 this.addOver();
				 this.flag=true; 
			 } 
		 }
	 },
	 
	 addStart(){
		 console.log('add start ');
		 console.log(window.score);
		this.flag=false; 
	 },
	 
	 addOver(){
		 
		 console.log('add over ... ');
		 
		 this.toScoreCallback = cc.callFunc(function () {
				this.showResult();
		}, this); 
		 
		 if(this.passLevel){
			 
			 this.messageDisplay.string = '过关成功';
			 
			 this.nextDisplay.string = '下个关卡';
			 
			 this.scoreDisplay.node.runAction(cc.sequence(cc.scaleTo(0.1,1.2,1.2),cc.scaleTo(0.1,1,1), this.toScoreCallback));
			 
			 if(
			 !cc.sys.localStorage.getItem("chapter"+(window.level))||
			 cc.sys.localStorage.getItem("chapter"+(window.level))<window.score
			 )
			 cc.sys.localStorage.setItem("chapter"+(window.level),window.score);
			 
			 if(window.level<9)
				 window.level++;
			 else
				window.level=0;
			 window.score=0;
			 window.lastScore = 0;
			 
		 }else{
			 
			 this.messageDisplay.string = '过关失败';
			 
			 this.nextDisplay.string = '再试一次';
			 
			 this.scoreDisplay.node.runAction(cc.sequence(cc.scaleTo(0.1,1.2,1.2),cc.scaleTo(0.1,1,1), this.toScoreCallback));
			 
			 //window.level=0;
			 window.score=0;
			 window.lastScore = 0;
			 
		 }
		 
		 
		 
	 },
	 
	 showResult(){
		this.messageDisplay.node.active = true;
		this.nextDisplay.node.active = true;
		this.backDisplay.node.active = true;
		/*
		if(this.score>=this.levelTarget)
			this.spriteManager.createNode(177.5,470,this.successSpriteFrame,0.7,this);
		else
			this.spriteManager.createNode(177.5,470,this.failSpriteFrame,0.7,this);*/
		this.nextDisplay.node.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5,1.2,1.2),cc.scaleTo(0.5,1,1))));
	 }
});
